<?php
/**
 * User: hjd-yao
 * Date: 2019/5/5
 * Time: 13:17
 */

namespace App\Http\Controllers\User;

use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\PlatFinanceModel;
use App\Models\Shop\OrderPaymentModel;
use App\Models\User\AccountFlowModel;
use App\Models\User\RechargeRecordModel;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RechargeController extends BackendController
{
    /**
     * @title 充值记录
     * @return View
     */
    public function rechargeList()
    {
        $list = RechargeRecordModel::with(['user'=>function($query){
            $query->select(['member_id','user_number','real_name','user_phone']);
        }])->has('user')->whereIn('status',[1,-1])->orderBy('created_at','desc')->paginate($this->getPerPageNum());

        return view('backend::account.recharge_list',[
            'page_title'=>'充值记录',
            'list'=>$list
        ]);
    }

    /**
     * @title 充值申请
     * @param Request $request
     * @return View
     */
    public function rgApply(Request $request)
    {
        $list = RechargeRecordModel::with(['user'=>function($query){
            $query->select(['member_id','user_number','real_name','user_phone']);
        }])->has('user')->where('status',0)->orderBy('created_at','desc')->paginate($this->getPerPageNum());

        return view('backend::account.recharge_apply',[
            'page_title'=>'充值申请',
            'list'=>$list
        ]);
    }

    /**
     * @title 充值详情
     * @param $id
     * @return View
     */
    public function detail($id)
    {
        $info = RechargeRecordModel::find($id);
        return view('backend::account.recharge_detail',[
            'page_title'=>'充值详情',
            'info'=>$info
        ]);
    }

    /**
     * @title 同意充值
     * @param $id
     * @return SelfResponse
     */
    public function rgAgree($id)
    {
        $info = RechargeRecordModel::find($id);
        if(!$info||$info->status!=0){
            return SelfResponse::error('申请信息不存在,或已处理');
        }
        $info->status = 1;
        $info->to_remark = '已充值';
        $info->handled_at = time();
        $money = abs($info->money);
        if($info->save()){
            $field = "money".$info->currency_id;
            $wallet = UserWalletModel::find($info->user_id);
            $data[$field]=DB::raw('IFNULL(`'.$field.'`,0)+'.$money);
            $data['total_rg'] = DB::raw('`total_rg`+'.$money);
            if(!UserWalletModel::where('member_id',$info->user_id)->update($data)){
                $info->status = 0;
                $info->to_remark = '充值失败';
                $info->save();
                return SelfResponse::error('充值审批失败,请稍后再试');
            }

            PlatFinanceModel::init()->countRgMoney($money);
            PlatFinanceModel::init()->incomeFinance($money);

            AccountFlowModel::init()->createFlow($info->user_id,$money,1,[
                'before_money'=>$wallet->$field,
                'change_type'=>39,
                'relate_uid'=>0,
                'money_type'=>$info->currency_id,
                'comment'=>'充值成功',
            ]);
            $orderPayment = new OrderPaymentModel();
            $orderPayment->where('out_trade_no',$info['rg_sn'])->update([
                'pay_status'=>1,
                'pay_at'=>time()
            ]);

            return SelfResponse::success('充值审批成功,请查收是否到账','b_Recharge_rechargeList');
        }
        return SelfResponse::error('充值审批失败,请稍后再试');
    }

    /**
     * @title 拒绝充值
     * @param Request $request
     * @param $id
     * @return SelfResponse
     */
    public function rgRefuse(Request $request,$id)
    {
        $info = RechargeRecordModel::find($id);
        if(!$info||$info->status!=0){
            return SelfResponse::error('申请信息不存在,或已处理');
        }
        $info->status = -1;
        $info->to_remark = $request->post('remark');
        $info->handled_at = time();
        if(!$info->save()){
            return SelfResponse::error('充值审批失败,请稍后再试');
        }
        $orderPayment = new OrderPaymentModel();
        $orderPayment->where('out_trade_no',$info['rg_sn'])->update([
            'pay_status'=>-1,
            'pay_at'=>time()
        ]);

        return SelfResponse::success('充值成功拒批','b_Recharge_rechargeList');
    }

    /**
     * @title 删除充值记录
     * @param Request $request
     * @return SelfResponse
     */
    public function delete(Request $request)
    {
        $ids = $request->input('rg_id');
        $ids = array_filter(is_array($ids)?$ids:str2arr($ids));
        if(!$ids){
            return SelfResponse::error('请选择要操作的数据');
        }
        if(RechargeRecordModel::whereIn('id',$ids)->whereIn('status',[-1,1])->delete()){
            return SelfResponse::success('删除成功','b_Recharge_rechargeList');
        }
        return SelfResponse::error('删除失败');
    }
}